package Sagittarius.Manager
{
	import com.pblabs.engine.debug.Logger;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**************************************************************************
	 * ...
	 * @author Aqua
	 *************************************************************************/
	public class ViewManager
	{
		//------------------- Singleton -------------------//
		/**************************************************************************
		 * function					instance getter
		 * description				a private function used to enforce singlton
		 * @return	ViewManager		singleton instance of this class
		 *************************************************************************/
		public static function get instance():ViewManager
		{
			if(null == _instance)
			{
				_instance = new ViewManager(singleton);
			}
		
			return _instance;
		}
		
		/**************************************************************************
		 * function				singleton
		 * description			a private function used to enforce singlton
		 *************************************************************************/
		private static function singleton():void
		{
			
		}
		//------------------- End Of Singleton -------------------//
		
		
		//------------------- Constructor -------------------//
		/**************************************************************************
		 * function				ViewManager
		 * description			constructor of class
		 * @param	s			a private function used to enforce singlton
		 *************************************************************************/
		public function ViewManager(s:Function = null):void
		{
			if (singleton != s)
			{
				throw new Error("This is a singleton class!");
			}
			
			_initialized = false;
		}
		//------------------- End Of Constructor -------------------//
		
		
		//------------------- Interface -------------------//
		/**************************************************************************
		 * function				initialize
		 * description			initialize data
		 * @param	container	all elements will be rendered to this container
		 * @param	width		width of the stage
		 * @param	heigth		height of the stage
		 *************************************************************************/
		public function initialize(container:Sprite, width:uint, height:uint):void
		{
			_container = container;
			_bmpDataFill = new BitmapData(width, height, true, 0x000000);
			
			if (null != _container)
			{
				_container.graphics.beginBitmapFill(_bmpDataFill);
				_container.graphics.drawRect(0, 0, width, height);
				_container.graphics.endFill();
				_initialized = true;
			}
		}
		
		/**************************************************************************
		 * function				renderImage
		 * description			render an image to screen
		 * @param	resourceId	the unique id of the image resource
		 * @param	pos			the position to be rendered at
		 *************************************************************************/
		public function renderImage(resourceId:String, pos:Point):void
		{
			if (! _initialized)
			{
				Logger.print(this, "Can't render before initialized!");
				return;
			}
			
			var bmpData:BitmapData;
			
			bmpData = ResourceManager.instance.getResource(resourceId) as BitmapData;
			_bmpDataFill.copyPixels(bmpData, bmpData.rect, pos, null, null, true);
		}
		//------------------- End Of Interface -------------------//
		
		
		private static var _instance:ViewManager;
		
		private var _initialized	:Boolean;
		private var _container		:Sprite;
		private var _bmpDataFill	:BitmapData;
	}
	
}